package org.yanfengying.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.yanfengying.common.EmilService;
import org.yanfengying.constants.BaseConstants;
import org.yanfengying.dto.ShopResouceDTO;
import org.yanfengying.org.domain.Employee;
import org.yanfengying.org.domain.Shop;
import org.yanfengying.org.domain.ShopAuditLog;
import org.yanfengying.org.mapper.EmployeeMapper;
import org.yanfengying.org.mapper.ShopAuditLogMapper;
import org.yanfengying.org.mapper.ShopMapper;
import org.yanfengying.org.service.IBaiduAiService;
import org.yanfengying.org.service.IShopService;
import org.yanfengying.utils.AjaxResult;
import org.yanfengying.utils.RedisUtils;
import org.yanfengying.utils.service.impl.BastServerImpl;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.mail.MessagingException;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanfengying
 * @since 2022-11-01
 */
@Service
public class ShopServiceImpl extends BastServerImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IBaiduAiService baiduAiService;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private EmilService emilService;
    /**
     * 店铺入驻业务逻辑
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) throws MessagingException {
        //校验
        Shop shopDb = shopMapper.loadByName(shop.getName());
        validate(shop,shopDb);
        //保存店铺->店铺状态:待审核
        if(shopDb != null && shopDb.getState().equals(BaseConstants.Shop.STATE_REJECT_AUDIT)){
            shop.setId(shopDb.getId());
        }
        shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shop.setRegisterTime(new Date());
        if(shopDb != null){//说明店铺存在就修改
            shopMapper.update(shop);
        }else {
            shopMapper.insert(shop);
        }
        //判断店长是否存在->如果已存在就使用以前的信息,如果不存在就新增
        Employee employee = employeeMapper.loadByName(shop.getAdmin().getUsername());

        if(employee != null){//存在就使用已有的信息
            shop.setAdminId(employee.getId());
        }else {//没有就先保存
            employeeMapper.insert(shop.getAdmin());
            shop.setAdminId(shop.getAdmin().getId());
        }
        //审核->调用自动审核方法->审核店铺名字和店铺地址
        String text = shop.getName() + " " + shop.getAddress();
        AjaxResult result = baiduAiService.audit(text, null, null);
        //审核以后修改店铺状态->通过:待激活  未通过:驳回
        if(result.getSuccess()){
            //获取一个随机验证码
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.substring(0, 4);
            RedisUtils.INSTANCE.set("uu", uuid, 30000);
            Long id = shop.getId();
            //通过以后->激活->给店长发送激活邮件
            emilService.sendPix("2042417159@qq.com","请激活","<a href="+"http://127.0.0.1:80/shop/sshop/"+id+uuid+">点击链接激活</a>","1730691672@qq.com");
            shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        }else {
            shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        }
        shopMapper.update(shop);


        //设置审核日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setShopId(shop.getId());
        //0 已通过 1 驳回
        shopAuditLog.setState(result.getSuccess()? 1: 0);
        shopAuditLog.setNote(result.getSuccess()?"审核通过":"审核失败,"+result.getMessage());
        shopAuditLogMapper.insert(shopAuditLog);

    }

    //人工审核->通过
    @Override
    @Transactional
    public void pass(ShopResouceDTO dto) {
        //获取店铺信息更改店铺状态
        Shop shop = shopMapper.loadById(dto.getKey());
        shop.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopMapper.update(shop);
        //添加日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(shop.getId());
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(dto.getValue());
        shopAuditLog.setNote(dto.getIntro());
        shopAuditLogMapper.insert(shopAuditLog);

    }
    //人工审核->驳回
    @Override
    @Transactional
    public void nopass(ShopResouceDTO dto) {
        //获取店铺信息更改店铺状态
        Shop shop = shopMapper.loadById(dto.getKey());
        shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopMapper.update(shop);
        //添加日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(shop.getId());
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(dto.getValue());
        shopAuditLog.setNote(dto.getIntro());
        shopAuditLogMapper.insert(shopAuditLog);

    }

    private void validate(Shop shop,Shop shopDb) {
        //参数非空校验,格式校验
        if(ObjectUtils.isEmpty(shop)){//对象是否为空
            throw new RuntimeException("输入错误,请检查后重新输入");
        }
        if(StringUtils.isEmpty(shop.getName())){//店铺名是否为空
            throw new RuntimeException("输入错误,请检查后重新输入");
        }
        if(StringUtils.isEmpty(shop.getAdmin())){//店长信息是否为空
            throw new RuntimeException("输入错误,请检查后重新输入");
        }
        //店铺校验->查看店铺是否存在,如果已经存在就返回错误信息
        //根据店铺名查询店铺是否存在
        if(shopDb != null){
            //如果店铺已存在就返回错误信息
            throw new RuntimeException("店铺已存在,请检查后重新输入");
        }
        //两次密码对比->不相等就返回错误信息
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new RuntimeException("两次密码不相等,请检查后重新输入");
        }
    }
}
